package com.ruoyi.system.utils;

import com.ruoyi.system.domain.SvgFontElementModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;


@Slf4j
public class SvgFontUtil {

    /**
     * 从svgXml中获取数据,并进行数据计算:(replacedSwitch1,SvgFontElementModel)
     *
     * @param svgContent
     * @return
     * @throws Exception
     */
    public static Map<String, SvgFontElementModel> getSvgFontElement(String svgContent) {
        Map<String, SvgFontElementModel> svgElementMap = new HashMap<>();
        try {
            // 预处理：移除非法字符
//            svgContent = removeInvalidCharacters(svgContent);

            // 解析SVG文本为DOM对象
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(svgContent.getBytes(StandardCharsets.UTF_8)));

            document.getDocumentElement().normalize();

            // 使用XPath查询目标元素
            XPathFactory xPathFactory = XPathFactory.newInstance();
            XPath xpath = xPathFactory.newXPath();
            String expression = "//switch/foreignObject/div"; // XPath表达式
            XPathExpression xPathExpression = xpath.compile(expression);
            NodeList nodeList = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node divNode = nodeList.item(i);
                if (divNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element divElement = (Element) divNode;

                    // Extract style attributes from the first div
                    String style = divElement.getAttribute("style");
                    String width = extractStyleValue(style, "width");
                    String marginLeft = extractStyleValue(style, "margin-left");
                    String paddingTop = extractStyleValue(style, "padding-top");

                    // XPath expression to find the third-level div element
                    String thirdLevelDivExpression = "div/div";
                    XPathExpression thirdLevelExpr = xpath.compile(thirdLevelDivExpression);
                    NodeList thirdLevelDivs = (NodeList) thirdLevelExpr.evaluate(divElement, XPathConstants.NODESET);

                    for (int j = 0; j < thirdLevelDivs.getLength(); j++) {
                        Node thirdLevelDivNode = thirdLevelDivs.item(j);
                        if (thirdLevelDivNode.getNodeType() == Node.ELEMENT_NODE) {
                            Element thirdLevelDivElement = (Element) thirdLevelDivNode;

                            // Extract transform scale from the third-level div
                            String nestedStyle = thirdLevelDivElement.getAttribute("style");
                            String fontSize = extractStyleValue(nestedStyle, "font-size");
                            String lineHeight = extractStyleValue(nestedStyle, "line-height");
                            String scale = extractTransformScale(nestedStyle);
                            String textContent = thirdLevelDivElement.getTextContent();
                            SvgFontElementModel fontElement = new SvgFontElementModel();
                            if (StringUtils.isNotEmpty(width)) {
                                fontElement.setWidth(Integer.parseInt(width.replace("px","" )));
                            }
                            if (StringUtils.isNotEmpty(marginLeft)) {
                                fontElement.setMarginLeft(Integer.parseInt(marginLeft.replace("px","" )));
                            }
                            if (StringUtils.isNotEmpty(paddingTop)) {
                                fontElement.setPaddingTop(Integer.parseInt(paddingTop.replace("px","" )));
                            }
                            if (StringUtils.isNotEmpty(scale)) {
                                // 如果为0或者0.0，则为1；
                               fontElement.setScale(Float.parseFloat(scale));
                               if(fontElement.getScale() == 0){
                                   fontElement.setScale(1);
                               }
                            }else{
                                fontElement.setScale(1);
                            }
                            if (StringUtils.isNotEmpty(fontSize)) {
                                fontElement.setFontSize(Integer.parseInt(fontSize.replace("px","" )));
                            }
                            if (StringUtils.isNotEmpty(lineHeight)) {
                                fontElement.setLineHeight(Float.parseFloat(lineHeight));
                            }
                            if (StringUtils.isNotEmpty(textContent)) {
                                fontElement.setTextContent(textContent);
                            }
                            svgElementMap.put("replacedSwitch" + i, fontElement);
                        }
                    }
                }
            }
        } catch (XPathExpressionException | ParserConfigurationException | IOException | SAXException e) {
            e.printStackTrace();
        }
        return svgElementMap;
    }


    /**
     * 从svgxml中获取数据，并组装成一个string字符串
     * flag:0原始svg  1特殊pdf处理
     * @param svgContent
     * @return
     * @throws Exception
     */

    public static String replaceSVGXml(String svgContent,Map<String, SvgFontElementModel> svgFontElementModelMap,int flag) {
        try {
//            // 预处理：移除非法字符
//            svgContent = removeInvalidCharacters(svgContent);

            // 解析SVG文本为DOM对象
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(svgContent.getBytes(StandardCharsets.UTF_8)));

            document.getDocumentElement().normalize();

            // 使用XPath查询目标元素
            XPathFactory xPathFactory = XPathFactory.newInstance();
            XPath xpath = xPathFactory.newXPath();
            String expression = "//switch"; // XPath表达式
            XPathExpression xPathExpression = xpath.compile(expression);
            NodeList switchNodes = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);

            for (int i = 0; i < switchNodes.getLength(); i++) {
                Node switchNode = switchNodes.item(i);
                SvgFontElementModel svgFontElementModel = svgFontElementModelMap.get("replacedSwitch"+i);
                if (switchNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element switchElement = (Element) switchNode;

                    // 查询<switch>标签下的<text>元素
                    NodeList textNodes = switchElement.getElementsByTagName("text");
                    for (int j = 0; j < textNodes.getLength(); j++) {
                        Node textNode = textNodes.item(j);
                        if (textNode.getNodeType() == Node.ELEMENT_NODE) {
                            Element textElement = (Element) textNode;
//                            // 获取 style 属性
//                            Integer fontSize = Integer.parseInt(textElement.getAttribute("font-size").replace("px", ""));
//                            svgFontElementModel.setFontSize(fontSize);
                            // 修改属性
                            textElement.setAttribute("font-size", svgFontElementModel.getFontSize()*svgFontElementModel.getScale()+"px");
                            textElement.setAttribute("x", (svgFontElementModel.getWidth()/2+svgFontElementModel.getMarginLeft())+"px");
                            if(flag == 0){
                                textElement.setAttribute("dominant-baseline", "text-before-edge");
                                textElement.setAttribute("y", (svgFontElementModel.getPaddingTop() + svgFontElementModel.getFontSize()*svgFontElementModel.getScale()/2)+"px");
                            }else{
                                textElement.setAttribute("dominant-baseline", "text-after-edge");
                                textElement.setAttribute("y", (svgFontElementModel.getPaddingTop() + svgFontElementModel.getFontSize()*svgFontElementModel.getLineHeight()*svgFontElementModel.getScale()/2)+"px");
                            }

                            if(textElement.getAttribute("font-family").equals("Helvetica") && containsChinese(svgFontElementModel.getTextContent())){
                                textElement.setAttribute("font-family","SimSun");
                            }
                            textElement.setTextContent(svgFontElementModel.getTextContent());

                            // 将修改后的<text>元素替换<switch>标签
                            Node parentNode = switchElement.getParentNode();
                            if (parentNode != null) {
                                parentNode.replaceChild(textElement, switchElement);
                            }
                        }
                    }
                }
            }

            // 将修改后的文档转换为字符串
            return nodeToString(document);
        } catch (XPathExpressionException | ParserConfigurationException | IOException | SAXException e) {
            e.printStackTrace();
        }
        return svgContent;
    }

    private static String extractStyleValue(String style, String property) {
        String[] styles = style.split(";");
        for (String s : styles) {
            String[] keyValue = s.split(":");
            if (keyValue.length == 2 && keyValue[0].trim().equals(property)) {
                return keyValue[1].trim();
            }
        }
        return null;
    }

    private static String extractTransformScale(String style) {
        String[] styles = style.split(";");
        for (String s : styles) {
            if (s.contains("transform")) {
                String[] transformParts = s.split("\\s+");
                for (String part : transformParts) {
                    if (part.startsWith("scale(")) {
                        String scaleValue = part.substring(6, part.length() - 1);
                        return scaleValue;
                    }
                }
            }
        }
        return null;
    }

    private static String removeInvalidCharacters(String input) {
        // 移除BOM标记
        input = input.replace("\uFEFF", "");
        // 移除非法的UTF-8字符
        return input.replaceAll("[^\\x00-\\x7F\\p{L}\\p{M}\\p{N}\\p{P}\\p{Z}\\p{Cf}\\p{Cs}\\s]", "");
    }

    private static String nodeToString(Node node) {
        try {
            StringWriter sw = new StringWriter();
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(node), new StreamResult(sw));
            return sw.toString();
        } catch (Exception ex) {
            throw new RuntimeException("Error converting node to string", ex);
        }
    }

    public static boolean containsChinese(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        return str.matches(".*[\u4e00-\u9fa5].*");
    }

//    public static void main(String[] args) {
////        String svgXml = "<g text-rendering=\"geometricPrecision\"><text pointer-events=\"visible\" text-anchor=\"middle\" dominant-baseline=\"text-before-edge\" line-height=\"1.2\" style=\"white-space: pre;\"><tspan font-size=\"8.5px\" font-family=\"Helvetica\" font-weight=\"normal\" font-style=\"0\" letter-spacing=\"0px\" x=\"22.5\" y=\"1\">2017大武汉24</tspan></text></g>";
////        String svgXml = "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\" width=\"100%\" height=\"100%\" viewBox=\"0 0 2245 1587\">\n" +
////                "<g text-rendering=\"geometricPrecision\"><text pointer-events=\"visible\" text-anchor=\"middle\" dominant-baseline=\"text-before-edge\" line-height=\"1.2\" style=\"white-space: pre;\"><tspan font-size=\"8.5px\" font-family=\"Helvetica\" font-weight=\"normal\" font-style=\"0\" letter-spacing=\"0px\" x=\"70.75\" y=\"1\">根据ABN\u007F1-EN-GDN-170093/R2修改</tspan></text></g>\n" +
////                "</svg>";
////        String svgXml = "<g><path d=\"M0 0 L98 0 L98 82 L0 82 Z\" style=\"fill: none; stroke-width: 1; stroke: rgb(0, 0, 0); pointer-events: none;\"></path><rect width=\"58\" height=\"20\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5800\" style=\"x: 0px; y: 0px; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"58\" height=\"20\" row=\"0\" col=\"0\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"0\" y=\"0\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">MA-02A<br>1-VXS-M6-004</div></div></foreignObject><rect width=\"20\" height=\"20\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5801\" style=\"x: 58; y: 0px; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"20\" height=\"20\" row=\"0\" col=\"1\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"58\" y=\"0\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">ST</div></div></foreignObject><rect width=\"20\" height=\"20\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5802\" style=\"x: 78; y: 0px; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"20\" height=\"20\" row=\"0\" col=\"2\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"78\" y=\"0\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">ST</div></div></foreignObject><rect width=\"58\" height=\"22\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5810\" style=\"x: 0px; y: 20; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"58\" height=\"22\" row=\"1\" col=\"0\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"0\" y=\"20\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">MA-02B<br>1-VXS-M6-004</div></div></foreignObject><rect width=\"20\" height=\"22\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5811\" style=\"x: 58; y: 20; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"20\" height=\"22\" row=\"1\" col=\"1\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"58\" y=\"20\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">ST</div></div></foreignObject><rect width=\"20\" height=\"22\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5812\" style=\"x: 78; y: 20; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"20\" height=\"22\" row=\"1\" col=\"2\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"78\" y=\"20\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">ST</div></div></foreignObject><rect width=\"58\" height=\"20\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5820\" style=\"x: 0px; y: 42; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"58\" height=\"20\" row=\"2\" col=\"0\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"0\" y=\"42\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">MA-02A<br>1-VXS-M6-004</div></div></foreignObject><rect width=\"20\" height=\"20\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5821\" style=\"x: 58; y: 42; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"20\" height=\"20\" row=\"2\" col=\"1\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"58\" y=\"42\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">SP</div></div></foreignObject><rect width=\"20\" height=\"20\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5822\" style=\"x: 78; y: 42; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"20\" height=\"20\" row=\"2\" col=\"2\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"78\" y=\"42\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">NOTE7</div></div></foreignObject><rect width=\"58\" height=\"20\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5830\" style=\"x: 0px; y: 62; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"58\" height=\"20\" row=\"3\" col=\"0\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"0\" y=\"62\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">MA-02B<br>1-VXS-M6-004</div></div></foreignObject><rect width=\"20\" height=\"20\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5831\" style=\"x: 58; y: 62; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"20\" height=\"20\" row=\"3\" col=\"1\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"58\" y=\"62\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">SP</div></div></foreignObject><rect width=\"20\" height=\"20\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_5832\" style=\"x: 78; y: 62; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"20\" height=\"20\" row=\"3\" col=\"2\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_58\" x=\"78\" y=\"62\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">NOTE7</div></div></foreignObject></g>";
////        svgXml = svgXml.replaceAll("<br>","<br/>");
////        System.out.println(replaceDivText(svgXml,1));
//
//       // String svgxml = "<path d=\"M822.86 393.95 L822.9 648 H592.2 L592.21 396.9\" pointer-events=\"stroke\" style=\"fill: none; stroke: rgb(0, 0, 0); stroke-width: 14; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 3.8; stroke-dashoffset: 0.7; stroke-opacity: 1; visibility: hidden;\"></path><circle r=\"1\" cx=\"822.86\" cy=\"393.95\" class=\"connect\" pointname=\"temp1\" temp=\"true\" style=\"fill-opacity: 0; stroke-width: 0; stroke-opacity: 0;\"></circle><circle r=\"1\" cx=\"592.21\" cy=\"396.9\" class=\"connect\" pointname=\"temp2\" temp=\"true\" style=\"fill-opacity: 0; stroke-width: 0; stroke-opacity: 0;\"></circle><path d=\"M822.86 393.95 L822.9 648 H592.2 L592.21 396.9\" pointer-events=\"stroke\" style=\"fill: none; stroke: rgb(0, 0, 0); stroke-width: 0.6665; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 3.8; stroke-dashoffset: 0.7; stroke-opacity: 1;\"></path><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1237.8545969585584,-407.8078734841383)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.8633333333333 415.12083333333334L828.8633333333333 407.12083333333334 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.8633333333333 423.12083333333334L822.8633333333333 415.12083333333334 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1259.028729477987,-386.64366531412384)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.8666666666667 436.29166666666663L828.8666666666667 428.29166666666663 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.8666666666667 444.29166666666663L822.8666666666667 436.29166666666663 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1280.202923022962,-365.4795181792649)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.87 457.4625L828.87 449.4625 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.87 465.4625L822.87 457.4625 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1301.377116567937,-344.315371044406)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.8733333333333 478.6333333333333L828.8733333333333 470.6333333333333 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.8733333333333 486.6333333333333L822.8733333333333 478.6333333333333 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1322.5512490873657,-323.1511628743916)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.8766666666667 499.8041666666667L828.8766666666667 491.8041666666667 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.8766666666667 507.8041666666667L822.8766666666667 499.8041666666667 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1343.725412114763,-301.98704625230584)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.88 520.975L828.88 512.975 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.88 528.975L822.88 520.975 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1364.899605659738,-280.8228991174469)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.8833333333333 542.1458333333333L828.8833333333333 534.1458333333333 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.8833333333333 550.1458333333333L822.8833333333333 542.1458333333333 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1386.0737381791669,-259.65869094743255)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.8866666666667 563.3166666666666L828.8866666666667 555.3166666666666 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.8866666666667 571.3166666666666L822.8866666666667 563.3166666666666 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1407.2479317241418,-238.4945438125735)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.89 584.4875L828.89 576.4875 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.89 592.4875L822.89 584.4875 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1428.4220642435705,-217.33033564255902)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.8933333333333 605.6583333333333L828.8933333333333 597.6583333333333 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.8933333333333 613.6583333333333L822.8933333333333 605.6583333333333 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00015744931904804717,0.9999999876048559,-0.9999999876048559,0.00015744931904804717,1449.5962577885455,-196.1661885077001)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M822.8966666666666 626.8291666666667L828.8966666666666 618.8291666666667 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M816.8966666666666 634.8291666666667L822.8966666666666 626.8291666666667 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(1,0,0,1,0,0)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M806.9000045058352 648L812.9000045058352 640 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M800.9000045058352 656L806.9000045058352 648 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(1,0,0,1,0,0)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M784.9000099909964 648L790.9000099909964 640 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M778.9000099909964 656L784.9000099909964 648 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(1,0,0,1,0,0)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M762.9000154761576 648L768.9000154761576 640 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M756.9000154761576 656L762.9000154761576 648 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(1,0,0,1,0,0)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M740.9000209613188 648L746.9000209613188 640 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M734.9000209613188 656L740.9000209613188 648 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(1,0,0,1,0,0)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M718.90002644648 648L724.90002644648 640 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M712.90002644648 656L718.90002644648 648 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(1,0,0,1,0,0)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M696.9000319316411 648L702.9000319316411 640 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M690.9000319316411 656L696.9000319316411 648 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(1,0,0,1,0,0)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M674.9000374168023 648L680.9000374168023 640 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M668.9000374168023 656L674.9000374168023 648 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(1,0,0,1,0,0)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M652.9000429019635 648L658.9000429019635 640 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M646.9000429019635 656L652.9000429019635 648 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(1,0,0,1,0,0)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M630.9000483871247 648L636.9000483871247 640 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M624.9000483871247 656L630.9000483871247 648 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(1,0,0,1,0,0)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M608.9000538722859 648L614.9000538722859 640 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M602.9000538722859 656L608.9000538722859 648 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,-34.897790307152036,1219.2508442827286)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.2008333333333 627.075L598.2008333333333 619.075 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.2008333333333 635.075L592.2008333333333 627.075 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,-13.971948072619398,1198.3275443147932)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.2016666666667 606.15L598.2016666666667 598.15 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.2016666666667 614.15L592.2016666666667 606.15 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,6.95395519706949,1177.4041833141323)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.2025000000001 585.225L598.2025000000001 577.225 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.2025000000001 593.225L592.2025000000001 585.225 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,27.879736398876616,1156.4808223110408)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.2033333333334 564.3L598.2033333333334 556.3 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.2033333333334 572.3L592.2033333333334 564.3 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,48.80557863340914,1135.5575223431053)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.2041666666667 543.375L598.2041666666667 535.375 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.2041666666667 551.375L592.2041666666667 543.375 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,69.73142086794189,1114.6342223751699)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.205 522.45L598.205 514.45 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.205 530.45L592.205 522.45 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,90.65723258732709,1093.7108308557154)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.2058333333334 501.525L598.2058333333334 493.525 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.2058333333334 509.525L592.2058333333334 501.525 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,111.58307482185973,1072.7875308877801)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.2066666666667 480.6L598.2066666666667 472.6 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.2066666666667 488.6L592.2066666666667 480.6 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,132.5089475739705,1051.8642004034818)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.2075 459.67499999999995L598.2075 451.67499999999995 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.2075 467.67499999999995L592.2075 459.67499999999995 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,153.43472877577756,1030.9408394003904)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.2083333333334 438.75L598.2083333333334 430.75 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.2083333333334 446.75L592.2083333333334 438.75 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g><g class=\"line-grass\" transform=\"matrix(0.00003982477097649671,-0.9999999992069938,0.9999999992069938,0.00003982477097649671,174.36057101031025,1010.017539432455)\"><path style=\"fill:none;stroke:#FFFFFF;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\"SvgjsPath6588\" sodipodi:nodetypes=\"cc\" d=\"M592.2091666666668 417.825L598.2091666666668 409.825 \"></path><path style=\"fill:none;stroke:#000000;stroke-width:0.6665;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" d=\"M586.2091666666668 425.825L592.2091666666668 417.825 \" id=\"SvgjsPath6587\" sodipodi:nodetypes=\"cc\"></path></g>";
//        String svgXml = "<g><path d=\"M0 0 L78 0 L78 10 L0 10 Z\" style=\"fill: none; stroke-width: 1; stroke: rgb(0, 0, 0); pointer-events: none;\"></path><rect width=\"58\" height=\"10\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_9200\" style=\"x: 0px; y: 0px; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"58\" height=\"10\" row=\"0\" col=\"0\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_92\" x=\"0\" y=\"0\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">EH-02</div></div></foreignObject><rect width=\"20\" height=\"10\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_9201\" style=\"x: 58; y: 0px; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"20\" height=\"10\" row=\"0\" col=\"1\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_92\" x=\"58\" y=\"0\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">MOD</div></div></foreignObject></g>";
//        // 1.获取数据集合
//        Map<String, SvgFontElementModel> svgFontElement = SvgFontUtil.getSvgFontElement(svgXml);
////         2.替换文本数据
//        SvgFontUtil.replaceSVGXml(svgXml,svgFontElement,1);
//    }


    public static boolean isValidXml(String xmlContent) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            builder.parse(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8)));
            return true;
        } catch (ParserConfigurationException | SAXException | IOException e) {
            return false;
        }
    }

    public static String replaceTextNote(String svgXml, int flag) {
        if(flag ==0){
            return svgXml;
        }

        try {
            // 预处理：移除非法字符
            svgXml = removeInvalidCharacters(svgXml);
            //换行符处理 空格符处理
            svgXml = svgXml.replaceAll("<br>", "<br/>").replace(" ", " ");
            if(!isValidXml(svgXml)){
                svgXml = "<g transform=\"matrix(1,0,0,1,0,0)\">"+svgXml +"</g>";
            }

            // 解析SVG文本为DOM对象
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(svgXml.getBytes(StandardCharsets.UTF_8)));

            document.getDocumentElement().normalize();
            // 使用XPath查询目标元素
            XPath xpath = XPathFactory.newInstance().newXPath();
            if (svgXml.contains("<foreignObject>") && svgXml.contains("</div>") && (!svgXml.contains("<switch>"))) {
                NodeList nodeList = (NodeList) xpath.evaluate("//foreignObject//div", document, XPathConstants.NODESET);

                for (int i = 0; i < nodeList.getLength(); i++) {
                    Element textElemnt = (Element) nodeList.item(i);
                    textElemnt.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
                }
            }

            // 文字标注处理
            if (svgXml.contains("<text>") && svgXml.contains("</tspan>")) {
                NodeList nodeList = (NodeList) xpath.evaluate("//text",document, XPathConstants.NODESET);
                for (int i = 0; i < nodeList.getLength(); i++) {
                    Element textElemnt = (Element) nodeList.item(i);
                    // 将text-before-edge计算方式转化为text-after-edge
                    if( textElemnt.getAttribute("dominant-baseline").equals("text-after-edge")){
                        continue;
                    }
                    textElemnt.setAttribute("dominant-baseline","text-after-edge");
//                float lineHeight =Float.parseFloat(textElemnt.getAttribute("line-height").replace("px","" ));

                    // 获取tspan的数据
                    NodeList tspanNodes = textElemnt.getElementsByTagName("tspan");

                    for (int j = 0; j < tspanNodes.getLength(); j++) {
                        Element tspanElement = (Element) tspanNodes.item(j);
                        //中文字体处理
                        if(tspanElement.getAttribute("font-family").equals("Helvetica") && containsChinese(tspanElement.getTextContent())){
                            tspanElement.setAttribute("font-family","SimSun");
                        }

                        // 坐标计算
                        float fontSize = Float.parseFloat(tspanElement.getAttribute("font-size").replace("px","" ));
                        if(tspanElement.getTextContent().contains("\u007F")){
//                        tspanElement.setTextContent("  "+tspanElement.getTextContent().replace("\u007F"," "));
                            tspanElement.setAttribute("x",Float.parseFloat(tspanElement.getAttribute("x").replace("px",""))-fontSize/2-fontSize/8+"px");
                        }else{
                            tspanElement.setTextContent(tspanElement.getTextContent());
                        }
                        float y =  Float.parseFloat(tspanElement.getAttribute("y").replace("px","" ));

                        String ySpan = y + fontSize +"px";
                        tspanElement.setAttribute("y",ySpan);
                    }
                }
            }

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
            transformer.setOutputProperty(OutputKeys.INDENT,"yes");

            StringWriter writer = new StringWriter();
            transformer.transform(new DOMSource(document),new StreamResult(writer));
            return writer.getBuffer().toString();
        } catch (XPathExpressionException | ParserConfigurationException | IOException | SAXException | TransformerException e) {
            e.printStackTrace();
        }

        return svgXml;
    }

    public static void main(String[] args) {
        String svgXml = "<g><path d=\"M0 0 L78 0 L78 10 L0 10 Z\" style=\"fill: none; stroke-width: 1; stroke: rgb(0, 0, 0); pointer-events: none;\"></path><rect width=\"58\" height=\"10\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_9200\" style=\"x: 0px; y: 0px; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"58\" height=\"10\" row=\"0\" col=\"0\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_92\" x=\"0\" y=\"0\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">EH-02</div></div></foreignObject><rect width=\"20\" height=\"10\" id=\"rectentity-0848106f41b74084b2b2e8184a45e055_9201\" style=\"x: 58; y: 0px; fill: none; stroke-width: 1; stroke: rgb(0, 0, 0);\"></rect><foreignObject width=\"20\" height=\"10\" row=\"0\" col=\"1\" tableid=\"entity-0848106f41b74084b2b2e8184a45e055_92\" x=\"58\" y=\"0\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:200%;height:100%;text-align: center;font-weight:undefined;line-height:1.2;font-size:14px;font-family:Helvetica;font-style:undefined;letter-spacing:0;transform: translate3d(-25%, 0px, 0px) scale(0.5);\"><div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">MOD</div></div></foreignObject></g>";
        System.out.println( replaceForeignObjectWithText(svgXml));
    }

    /**
     * 处理foreignObject中的div文本，不包含switch标签
     * @param svgXml
     * @return
     */

    public static String replaceForeignObjectWithText(String svgXml) {
        try {
            // 解析SVG文本为DOM对象
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(svgXml.getBytes(StandardCharsets.UTF_8)));

            document.getDocumentElement().normalize();

            // 使用XPath查询目标元素
            XPath xpath = XPathFactory.newInstance().newXPath();
            NodeList foreignObjectNodes = (NodeList) xpath.evaluate("//foreignObject", document, XPathConstants.NODESET);

            for (int i = 0; i < foreignObjectNodes.getLength(); i++) {
                Node foreignObjectNode = foreignObjectNodes.item(i);
                if (foreignObjectNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element foreignObjectElement = (Element) foreignObjectNode;

                    // 1. 获取foreignObject标签的width属性、height属性
                    String width = foreignObjectElement.getAttribute("width");
                    String height = foreignObjectElement.getAttribute("height");

                    // 2. 获取foreignObject标签下的第一个div标签的line-height、font-size、font-family、transform中的scale的值
                    NodeList divNodes = foreignObjectElement.getElementsByTagName("div");
                    if (divNodes.getLength() > 0) {
                        Element firstDivElement = (Element) divNodes.item(0);
                        String style = firstDivElement.getAttribute("style");
                        String lineHeight = extractStyleValue(style, "line-height");
                        String fontSize = extractStyleValue(style, "font-size");
                        String fontFamily = extractStyleValue(style, "font-family");
                        String scale = extractTransformScale(style);

                        // 3. 获取foreignObject标签下的第一个div标签的里面的第一个div标签，获取内容
                        NodeList innerDivNodes = firstDivElement.getElementsByTagName("div");
                        String textContent = "";
                        if (innerDivNodes.getLength() > 0) {
                            Element innerDivElement = (Element) innerDivNodes.item(0);
                            textContent = innerDivElement.getTextContent();
                        }

                        // 4. 使用新的<text>标签,设置上述的line-height、font-size、font-family属性值以及内容，替换foreignObject标签
                        Element textElement = document.createElement("text");
                        textElement.setAttribute("line-height", lineHeight);
                        textElement.setAttribute("font-size", fontSize);
                        textElement.setAttribute("font-family", fontFamily);
                        textElement.setTextContent(textContent);

                        // 计算x和y属性
                        float fontSizeValue = Float.parseFloat(fontSize.replace("px", ""));
                        float x = Float.parseFloat(width) / 2;
                        float y = Float.parseFloat(height) - fontSizeValue ;
                        textElement.setAttribute("x", String.valueOf(x) + "px");
                        textElement.setAttribute("y", String.valueOf(y) + "px");
                        textElement.setAttribute("dominant-baseline","text-after-edge");
                        textElement.setAttribute("text-anchor","middle");
                        textElement.setAttribute("style","font-size:"+fontSize+";font-family:"+fontFamily+";line-height:"+lineHeight);

                        // 替换foreignObject标签
                        Node parentNode = foreignObjectNode.getParentNode();
                        if (parentNode != null) {
                            parentNode.replaceChild(textElement, foreignObjectNode);
                        }
                    }
                }
            }

            // 5. 返回新的svgxml
            return nodeToString(document);
        } catch (XPathExpressionException | ParserConfigurationException | IOException | SAXException e) {
            e.printStackTrace();
        }

        return svgXml;
    }


}
